<?php

namespace App\Admin\Repositories;

use App\Models\AdminUser;
use App\Models\Project as Model;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Repositories\EloquentRepository;
use Illuminate\Database\Eloquent\Builder;

class Project extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    public function get(Grid\Model $model)
    {
        $currLoginUser = auth('admin')->user();
        $where = null;
        if ((!Admin::user()->isAdministrator())) {
            $where = function (Builder $query) use ($currLoginUser) {

                $query->where('master_user_id', $currLoginUser->id)
                    ->orWhereRaw("FIND_IN_SET(" . $currLoginUser->id . ", worker_user_id)");
            };
        }

        $company_id = request()->input('company_id');
        if (!empty(request()->input('company_id'))) {

            $where = function (Builder $query) use ($company_id) {
                $query->where('company_id', $company_id);
            };
        }

        $params = request()->input();

        if (isset($params['per_page']) && !empty($params['per_page'])) {
            $per_page = $params['per_page'];
        } else {
            $per_page = 20;
        }

        return \App\Models\Project::where($where)
            ->orderBy('id', 'desc')
            ->paginate($per_page);
    }

    // 获取项目的负责人
    public static function getWorkerList()
    {
        return AdminUser::select('id', 'name')
            ->pluck('name', 'id');
    }

    public static function getFeeTypeOptions()
    {
        return \App\Models\ExpenseType::where('status', 1)
            ->pluck('title', 'id');
    }

    public static function getStatusOptions()
    {
        return [
            0 => '待完结',
            1 => '已完结'
        ];
    }

    public static function getBefoeAfterTax()
    {
        return [
            1 => '税后',
            2 => '税前',
        ];
    }
}
